﻿using System.Linq;
using System.Reflection;
using System.Web.Http;
using Abp.Application.Services;
using Abp.Configuration.Startup;
using Abp.Modules;
using Abp.WebApi;
using Swashbuckle.Application;
using System.Web.Http.Cors;
using Abp.Json;
using Abp.WebApi.Configuration;
using Abp.WebApi.Controllers.Dynamic;
using Castle.MicroKernel.Registration;
using Abp.Logging;

namespace LeadThing.AbpZeroTemplate.WebApi
{
    /// <summary>
    /// Web API layer of the application.
    /// </summary>
    [DependsOn(typeof(AbpWebApiModule), typeof(AbpZeroTemplateApplicationModule))]
    public class AbpZeroTemplateWebApiModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            //Automatically creates Web API controllers for all application services of the application
            Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(AbpZeroTemplateApplicationModule).Assembly, "app")
                .Build();

            Configuration.Modules.AbpWebApi().HttpConfiguration.Filters.Add(new HostAuthenticationFilter("Bearer"));

            //跨域配置
            var cors = new EnableCorsAttribute("*", "*", "*");
            GlobalConfiguration.Configuration.EnableCors(cors);

            //进行时间格式化
            //var converters = Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.Converters;

            //foreach (var converter in converters)
            //{
            //    if (converter is AbpDateTimeConverter)
            //    {
            //        var tmpConverter = converter as AbpDateTimeConverter;
            //        tmpConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            //    }
            //}

            ConfigureSwaggerUi(); //Remove this line to disable swagger UI.
        }

        //public override void PostInitialize()
        //{
        //    var httpConfiguration = IocManager.Resolve<IAbpWebApiConfiguration>().HttpConfiguration;
        //    InitializeRoutes(httpConfiguration);

        //    Configuration.Modules.AbpWebApi().HttpConfiguration.EnsureInitialized();
        //}

        //private static void InitializeRoutes(HttpConfiguration httpConfiguration)
        //{
        //    //Custom routes
        //    httpConfiguration.Routes.MapHttpRoute(
        //        name: "CustomApi",
        //        routeTemplate: "actionapi/{controller}/{action}/{id}",
        //        defaults: new { id = RouteParameter.Optional }
        //    );
        //}

        private void ConfigureSwaggerUi()
        {
            Configuration.Modules.AbpWebApi().HttpConfiguration
                .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "LeadThing.AbpZeroTemplate.WebApi");
                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                })
                .EnableSwaggerUi(c =>
                {
                    c.InjectJavaScript(Assembly.GetAssembly(typeof(AbpZeroTemplateWebApiModule)), "LeadThing.AbpZeroTemplate.WebApi.Scripts.Swagger-Custom.js");
                });
        }
    }
}
